<script setup lang="ts">
import Condition from '@/components/Condition/index.vue'
import APISearch from '@/api/search'
import PaginationAlbum from '@/components/PaginationAlbum/index.vue'
import { SortDown, SortUp } from '@element-plus/icons-vue'
import ItemVideo from '@/components/ItemVideo/index.vue'

const $router = useRouter()
const $route = useRoute()

const queryParams = ref<any>({
    page: 1,
    limit: 8
})

const queryData = ref<any>({
    type: 'video',
    order: {
        sales: undefined,
        min_price: undefined,
        create_time: undefined
    }
})

const loading = shallowRef<boolean>(false)

watch($route, () => {
    getList()
    getConditionSelect()
    getAiSearch()
}, { immediate: true })


const list = ref<any>([])
const total = shallowRef<number>(0)

function getList() {
    queryData.value.keyword = $route.query.q
    const data = { ...queryData.value }
    data.order = JSON.stringify(data.order)
    loading.value = true
    APISearch.searchShop(data, queryParams.value).then(({ data, total: count }) => {
        list.value = data
        total.value = count
    }).finally(() => {
        loading.value = false
    })
}

const columns = ref([{
    placeholder: '类型',
    value: 'category_id',
    item: []
}, {
    placeholder: '横竖屏',
    value: 'hv_screens',
    item: []
}, {
    placeholder: '分辨率',
    value: 'resolving_power',
    item: []
}, {
    placeholder: '时长',
    value: 'duration',
    item: []
}, {
    placeholder: '格式',
    value: 'format',
    item: []
}])

function getConditionSelect() {
    APISearch.getConditionSelect('video').then(({ data }) => {
        columns.value[0].item = data.category_id
        columns.value[1].item = data.hv_screens
        columns.value[2].item = data.resolving_power
        columns.value[3].item = data.duration
        columns.value[4].item = data.format
    })
}

function handleJumpVideoDetails(id: number) {
    $router.push({
        name: 'VideoDetails',
        params: {
            id
        },
        query: {
            categoryType: 'Video'
        }
    })
}
function onSelectChange(value: Ref) {
    const filter: any = {}
    const op: any = {}
    for (let item in value.value) {
        filter[item] = JSON.parse(value.value[item]).type
        op[item] = JSON.parse(value.value[item]).op
    }
    queryData.value.filter = JSON.stringify(filter)
    queryData.value.op = JSON.stringify(op)
    categoryId.value = undefined
    activeId.value = undefined
    getList()
}

function handleChangeSort(type: string) {
    if (type == 'sales') {
        if (queryData.value.order.sales == undefined) {
            queryData.value.order.sales = 'desc'
        } else if (queryData.value.order.sales == 'desc') {
            queryData.value.order.sales = 'asc'
        } else if (queryData.value.order.sales == 'asc') {
            queryData.value.order.sales = undefined
        }
    } else if (type == 'min_price') {
        if (queryData.value.order.min_price == undefined) {
            queryData.value.order.min_price = 'desc'
        } else if (queryData.value.order.min_price == 'desc') {
            queryData.value.order.min_price = 'asc'
        } else if (queryData.value.order.min_price == 'asc') {
            queryData.value.order.min_price = undefined
        }
    } else if (type == 'create_time') {
        if (queryData.value.order.create_time == undefined) {
            queryData.value.order.create_time = 'desc'
        } else if (queryData.value.order.create_time == 'desc') {
            queryData.value.order.create_time = 'asc'
        } else if (queryData.value.order.create_time == 'asc') {
            queryData.value.order.create_time = undefined
        }
    }
    getList()
}

const categoryList = ref<any[]>([])

const ai = ref<any>({})

function getAiSearch() {
    const data: any = {
        keyword: $route.query.q,
        type: 'video'
    }
    APISearch.getAiSearch(data).then(({ data }) => {
        categoryList.value = data.category_id
        ai.value = data.ai
    })
}

const categoryId = shallowRef<number>()

const categoryIdList: any = computed(() => {
    const data = categoryList.value.filter((item: any) => item.id == categoryId.value)

    if (data.length != 0) {
        return data[0].children
    }

    return []
})

const activeId = shallowRef<number>()

watch(activeId, (newValue) => {
    queryData.value.category_id = newValue
    getList()
})

</script>

<template>
    <div class="search">
        <div class="search-api">
            <div class="title">AI相关推荐 <span>AI RELATED RECOMMEND</span></div>
            <div class="content">
                <div class="bg" :style="`background-image: url(${ai.image});`">
                    <div class="content-title">{{ ai.title }}</div>
                </div>
            </div>
        </div>
        <div class="search-classify">
            <div class="title">视频分类</div>
            <div>
                <div class="classify">
                    <div class="classify-item" v-for="item in categoryList" :key="item.id"
                        :class="{ active: item.id == categoryId }" @click="categoryId = item.id">{{ item.title }}</div>
                </div>
                <div class="classify-bottom">
                    <div class="classify-bottom-item" v-for="(item, index) in categoryIdList" :key="item.id"
                        @click="activeId = item.id" :class="{ 'active-item': activeId == item.id }">
                        {{ item.title }}
                        <div v-if="index != categoryIdList.length - 1"></div>
                    </div>
                </div>
            </div>
        </div>

        <div class="search-condition">
            <div class="title">搜索结果：<span>{{ total }}</span> 个视频</div>
            <Condition :columns="columns" @onSelectChange="onSelectChange" />
            <div class="sort">
                <div class="sort-conter">
                    <div @click="handleChangeSort('sales')">
                        销量
                        <el-icon
                            :color="queryData.order.sales == 'desc' && queryData.order.sales != 'asc' ? '#ff0000' : '#747474'">
                            <SortDown />
                        </el-icon>
                        <el-icon
                            :color="queryData.order.sales == 'asc' && queryData.order.sales != 'desc' ? '#ff0000' : '#747474'">
                            <SortUp />
                        </el-icon>
                    </div>
                    <div @click="handleChangeSort('min_price')">
                        价格
                        <el-icon
                            :color="queryData.order.min_price == 'desc' && queryData.order.min_price != 'asc' ? '#ff0000' : '#747474'">
                            <SortDown />
                        </el-icon>
                        <el-icon
                            :color="queryData.order.min_price == 'asc' && queryData.order.min_price != 'desc' ? '#ff0000' : '#747474'">
                            <SortUp />
                        </el-icon>
                    </div>
                    <div @click="handleChangeSort('create_time')">
                        时间
                        <el-icon
                            :color="queryData.order.create_time == 'desc' && queryData.order.create_time != 'asc' ? '#ff0000' : '#747474'">
                            <SortDown />
                        </el-icon>
                        <el-icon
                            :color="queryData.order.create_time == 'asc' && queryData.order.create_time != 'desc' ? '#ff0000' : '#747474'">
                            <SortUp />
                        </el-icon>
                    </div>
                </div>
            </div>
        </div>
        <div v-loading="loading">
            <el-empty description="暂无内容" v-if="list.length == 0" />
            <div class="list">
                <div class="item" v-for="item in list" :key="item" @click="handleJumpVideoDetails(item.id)">
                    <div class="frame">
                        <ItemVideo class="frame-item" :image="item.image" :sample="item.sample" />
                        <div class="frame-item" :style="{ backgroundImage: `url('${item.sprite}')` }"></div>
                    </div>
                </div>
            </div>
        </div>
        <PaginationAlbum :total="total" v-model:current-page="queryParams.page" :page-size="queryParams.limit"
            @get-list="getList" />
    </div>
</template>

<style lang="scss" scoped>
.search {
    box-sizing: border-box;
    width: 1340px;
    padding: 30px 20px;
    margin: 0 auto;

    .search-api {
        .content {
            margin-top: 18px;
            margin-bottom: 22px;

            .bg {
                width: 190px;
                height: 127px;
                border-radius: 5px;
                background-color: #eeeeee;
                display: flex;
                align-items: center;
                justify-content: center;
                background-size: cover;
                background-position: center;

                .content-title {
                    color: rgba(246, 246, 246, 1);
                    font-size: 14px;
                }
            }
        }

        .title {
            color: rgba(187, 187, 187, 1);
            font-size: 16px;
            display: flex;
            align-items: center;

            &::before {
                content: '';
                display: block;
                width: 5px;
                height: 16px;
                border-radius: 3px;
                background-color: rgba(249, 20, 20, 1);
                margin-right: 10px;
            }

            span {
                color: #BBBBBB;
                font-size: 12px;
                margin-left: 14px;
            }
        }
    }

    .search-classify {
        display: flex;

        .classify-bottom {
            display: flex;
            flex-wrap: wrap;
            margin-left: 20px;
            color: rgb(187, 187, 187);
            font-family: 'SOURCEHANSANSCN-EXTRALIGHT';

            .classify-bottom-item {
                cursor: pointer;
                display: flex;
                align-items: center;
                line-height: 1em;
                height: 20px;
                margin-bottom: 15px;

                div {
                    height: 20px;
                    width: 1px;
                    background-color: rgb(187, 187, 187);
                    margin: 0 10px;
                }

                &:first-child {
                    padding-left: 0;
                }

                &:last-child {
                    padding-right: 0;
                    border-right: none;

                }

            }

            .active-item {
                color: #FA3131;
            }
        }

        .classify {
            display: flex;
            gap: 0 20px;
            margin-left: 20px;
            color: rgb(187, 187, 187);
            box-sizing: border-box;
            margin-bottom: 20px;

            .classify-item {
                padding-bottom: 2px;
                cursor: pointer;
                font-family: 'SOURCEHANSANSCN-LIGHT';
            }

            .active {
                color: #FA3131;
                border-bottom: 2px solid #FA3131;
                padding-bottom: 0;
            }

        }

        .title {
            color: rgb(187, 187, 187);
            font-size: 16px;
            display: flex;
            align-items: center;
            height: 24px;
            white-space: nowrap;
            font-family: 'SOURCEHANSANSCN-LIGHT';

            &::before {
                content: '';
                display: block;
                width: 5px;
                height: 16px;
                border-radius: 3px;
                background-color: rgba(249, 20, 20, 1);
                margin-right: 10px;
            }

            span {
                color: rgba(249, 20, 20, 1);
                font-size: 16px;
            }
        }
    }

    .list {
        border-radius: 5px;
        position: relative;
        z-index: 5;
        grid-template-columns: repeat(4, 1fr);
        display: flex;
        flex-wrap: wrap;
        gap: 33px;

        .item {
            width: 300px;
            cursor: pointer;
            border-radius: 5px;
            overflow: hidden;

            .frame {
                background-color: red;
                height: 340px;

                .frame-item {
                    height: 170px;
                    background-color: #e8e8e8;
                    background-size: cover;
                    background-position: center;
                    position: relative;

                    .title {
                        position: absolute;
                        left: 20px;
                        bottom: 0;
                        height: 45px;
                        line-height: 45px;
                        font-size: 14px;
                        white-space: nowrap;
                        overflow: hidden;
                        text-overflow: ellipsis;
                        color: #ffffff;
                    }

                }
            }
        }
    }

    .search-condition {
        display: flex;
        align-items: center;

        .sort {
            display: flex;
            align-items: center;
            margin-left: auto;
            font-family: 'SOURCEHANSANSCN-EXTRALIGHT';

            &::before {
                content: '排序：';
                color: rgba(187, 187, 187, 1);
                font-size: 14px;
            }

            .sort-conter {
                display: flex;
                gap: 0 20px;

                div {
                    color: rgba(187, 187, 187, 1);
                    font-size: 14px;
                    display: flex;
                    align-items: center;
                    cursor: pointer;
                }
            }
        }

        .title {
            color: rgba(187, 187, 187, 1);
            font-size: 16px;
            display: flex;
            align-items: center;
            font-family: 'SOURCEHANSANSCN-EXTRALIGHT';

            &::before {
                content: '';
                display: block;
                width: 5px;
                height: 16px;
                border-radius: 3px;
                background-color: rgba(249, 20, 20, 1);
                margin-right: 10px;
            }

            span {
                color: rgba(249, 20, 20, 1);
                font-size: 16px;
            }
        }
    }
}
</style>
